
//
//  XJCommentHeaderView.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/6/20.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJCommentHeaderView.h"
#import "UIImage+ColorImage.h"
#import "XJAfterSaleModel.h"

@interface XJCommentHeaderView()
@property(nonatomic, strong) UIView *div1;
@property(nonatomic, strong) UIImageView *iconIv; //头像
@property(nonatomic, strong) UILabel *nameLb; //商品名
@property(nonatomic, strong) UILabel *priceLb;//价格
@property(nonatomic, strong) UILabel *countLb;//数量

@property(nonatomic, strong) UIView *div2; //三个button
@property(nonatomic, strong) UIButton *haoButton;
@property(nonatomic, strong) UIButton *zhongButton;
@property(nonatomic, strong) UIButton *chaButton;



@end

@implementation XJCommentHeaderView

// 200 = 100 + 50 + 50
#pragma mark - 一 lazy
- (UIView *)div1 {
    if (!_div1) {
        _div1 = [UIView new];
    }
    return _div1;
}
- (UIImageView *)iconIv {
    if (!_iconIv) {
        _iconIv=[UIImageView new];
        _iconIv.backgroundColor = [UIColor redColor];
    }
    return _iconIv;
}
- (UILabel *)nameLb {
    if (!_nameLb) {
        _nameLb = [UILabel  new];
        _nameLb.text = @"iOS测试商品1";
    }
    return _nameLb;
}
- (UILabel *)priceLb {
    if (!_priceLb) {
        _priceLb = [UILabel new];
        _priceLb.text = @"￥10.0";
    }
    return _priceLb;
}
- (UILabel *)countLb {
    if(!_countLb){
        _countLb = [UILabel  new];
        _countLb.text = @"x1";
    }
    return _countLb;
}

// ------------------------------------
#pragma mark - 一-2 lazy
- (UIView *)div2 {
    if (!_div2) {
        _div2 = [UIView new];
    }
    return _div2;
}

- (UIButton *)haoButton {
    if (!_haoButton) {
        _haoButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_haoButton setTitle:@"好评" forState:(UIControlStateNormal)];
        [_haoButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_haoButton setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateSelected)];
        
        [_haoButton setImage:[UIImage imageNamed:@"comment_hao_on"] forState:(UIControlStateSelected)];
        [_haoButton setImage:[UIImage imageNamed:@"comment_hao_off"] forState:(UIControlStateNormal)];
        
        _haoButton.layer.cornerRadius = 10;
        _haoButton.layer.masksToBounds = YES;
        _haoButton.layer.borderColor = [UIColor blackColor].CGColor;
        _haoButton.layer.borderWidth = 1.0;
        [_haoButton addTarget:self action:@selector(haoButonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        
        
        UIImage *yellowImg = [UIImage imageWithColor:XJCOLOR_RGB(255, 218, 67)];
        [_haoButton setBackgroundImage:yellowImg forState:(UIControlStateSelected)];
    }
    return _haoButton;
}
- (UIButton *)zhongButton {
    if (!_zhongButton) {
        
        _zhongButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_zhongButton setTitle:@"中评" forState:(UIControlStateNormal)];
        [_zhongButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_zhongButton setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateSelected)];
        
        [_zhongButton setImage:[UIImage imageNamed:@"comment_zhong_on"] forState:(UIControlStateSelected)];
        [_zhongButton setImage:[UIImage imageNamed:@"comment_zhong_off"] forState:(UIControlStateNormal)];
        
        _zhongButton.layer.cornerRadius = 10;
        _zhongButton.layer.masksToBounds = YES;
        _zhongButton.layer.borderColor = [UIColor blackColor].CGColor;
        _zhongButton.layer.borderWidth = 1.0;
        [_zhongButton addTarget:self action:@selector(zhongButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        UIImage *yellowImg = [UIImage imageWithColor:XJCOLOR_RGB(255, 218, 67)];
        [_zhongButton setBackgroundImage:yellowImg forState:(UIControlStateSelected)];

    }
    return _zhongButton;
}
- (UIButton *)chaButton {
    if (!_chaButton) {
        _chaButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_chaButton setTitle:@"差评" forState:(UIControlStateNormal)];
        [_chaButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_chaButton setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateSelected)];
        
        [_chaButton setImage:[UIImage imageNamed:@"comment_cha_on"] forState:(UIControlStateSelected)];
        [_chaButton setImage:[UIImage imageNamed:@"comment_cha_off"] forState:(UIControlStateNormal)];
        
        _chaButton.layer.cornerRadius = 10;
        _chaButton.layer.masksToBounds = YES;
        _chaButton.layer.borderColor = [UIColor blackColor].CGColor;
        _chaButton.layer.borderWidth = 1.0;
        [_chaButton addTarget:self action:@selector(chaButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        UIImage *yellowImg = [UIImage imageWithColor:XJCOLOR_RGB(255, 218, 67)];
        [_chaButton setBackgroundImage:yellowImg forState:(UIControlStateSelected)];
    }
    return _chaButton;
}

#pragma mark - 一 lazy-点击事件
- (void)haoButonAction:(UIButton *)btn{
    btn.selected = !btn.selected;
    
    self.zhongButton.selected = NO;
    self.chaButton.selected = NO;
    
    self.commentState = @"1"; //好评

}

- (void)zhongButtonAction:(UIButton *)btn{
    btn.selected = !btn.selected;
    
    self.haoButton.selected = NO;
    self.chaButton.selected = NO;
    
    self.commentState = @"2"; //中评

}
- (void)chaButtonAction:(UIButton *)btn {
    btn.selected = !btn.selected;
    
    self.haoButton.selected = NO;
    self.zhongButton.selected = NO;
    
    self.commentState = @"3"; //差评

}


#pragma mark - 一-3 lazy
- (YYTextView *)commentTextView {
    if(!_commentTextView){
        _commentTextView = [[YYTextView alloc] init];
        _commentTextView.font = XJFONTSET_R(15.0);
        _commentTextView.placeholderFont = XJFONTSET_R(15.0);
        _commentTextView.placeholderText = @"点击输入您的评价";
        
//        _commentTextView.layer.borderColor = [UIColor blackColor].CGColor;
//        _commentTextView.layer.borderWidth = 1.0;                                              
    }
    return _commentTextView;
}



#pragma mark - 二 init

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}
- (void)setupView {
    [self addSubview:self.div1];
    [self.div1 addSubview:self.iconIv];
    [self.div1 addSubview:self.nameLb];
    [self.div1 addSubview:self.priceLb];
    [self.div1 addSubview:self.countLb];


    
    [self addSubview:self.div2];
    [self.div2 addSubview:self.haoButton];
    
    self.haoButton.selected = YES; //默认好评
    self.commentState = @"1";
    
    [self.div2 addSubview:self.zhongButton];
    [self.div2 addSubview:self.chaButton];
    
    
    
    [self addSubview:self.commentTextView];
    
}
#pragma mark - 三 layout
//h:200
- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat k15 = 15.0;
    
    //h:100
    [self.div1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.left.equalTo(self.mas_left).offset(k15);
        make.right.equalTo(self.mas_right).offset(-k15);
        make.height.mas_equalTo(100);
    }];
    [self.iconIv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div1);
        make.centerY.equalTo(self.div1.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(80, 80));
    }];
    
    [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconIv.mas_right).offset(10);
        make.top.equalTo(self.div1);
        make.size.mas_equalTo(CGSizeMake(200, 30));
    }];
    [self.priceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconIv.mas_right).offset(10);
        make.bottom.equalTo(self.div1.mas_bottom).offset(-2);
        make.size.mas_equalTo(CGSizeMake(100, 20));
    }];
    [self.countLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.div1.mas_right);
        make.bottom.equalTo(self.div1.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(100, 20));
    }];
    
    
    
    
    //三个按钮的背景view
    [self.div2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.div1.mas_bottom);
        make.left.equalTo(self.mas_left).offset(k15);
        make.right.equalTo(self.mas_right).offset(-k15);
        make.height.mas_equalTo(50);
    }];
    
    CGFloat kMargin = 20;
    CGFloat kbutton_w = (KWidth - k15*2 - kMargin*2) / 3;
    CGFloat kbutton_h = 30;
    [self.haoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div2);
        make.centerY.equalTo(self.div2.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(kbutton_w,kbutton_h));
    }];
    [self.zhongButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.haoButton.mas_right).offset(kMargin);
        make.centerY.equalTo(self.div2.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(kbutton_w,kbutton_h));

    }];
    [self.chaButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.zhongButton.mas_right).offset(kMargin);
        make.centerY.equalTo(self.div2.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(kbutton_w,kbutton_h));
    }];
    
    
    
    
    [self.commentTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.div2.mas_bottom);
        make.left.equalTo(self.mas_left).offset(k15);
        make.right.equalTo(self.mas_right).offset(-k15);
        make.height.mas_equalTo(50);
    }];
}



#pragma mark - 赋值
- (void)setModel:(XJAfterSaleModel *)model {
    
    _model = model;
    
    UIImage *placehoderImg = [UIImage imageNamed:@"default_goods"];
    
    [self.iconIv sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:placehoderImg];
    self.nameLb.text = model.goods_name;
    
    self.priceLb.text = [NSString stringWithFormat:@"￥%@",model.price];
    self.countLb.text = [NSString stringWithFormat:@"x %@",model.quantity];
}
@end
